package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fz.entity.CateStores;
import com.fz.entity.RoleRes;
import com.fz.mapper.CateStoresMapper;
import com.fz.service.CateStoresService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品门店关联表 服务实现类
 * </p>
 *
 * @author fz
 * @since 2024-08-27
 */
@Service
public class CateStoresServiceImpl extends ServiceImpl<CateStoresMapper, CateStores> implements CateStoresService {
    @Override
    @Transactional
    public boolean save(Long storesId, Long[] menuIds, Long[] btnIds) {
        UpdateWrapper<CateStores> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("stores_id", storesId);
        this.remove(updateWrapper);
        List<CateStores> list = new ArrayList<>();
        if(null != menuIds){
            for(Long cateId : menuIds) {
                list.add(new CateStores(storesId,cateId, 1));
            }
        }
        if(null != btnIds){
            for(Long cateId : btnIds) {
                list.add(new CateStores(storesId,cateId, 2));
            }
        }
        return this.saveBatch(list);
    }

    @Override
    public List<CateStores> getByStoresId(Long storesId) {
        QueryWrapper<CateStores> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stores_id", storesId);
        return this.list(queryWrapper);
    }
}
